<?php



namespace App\Services\Business\Models;


use App\Services\Member\Models\Activity;
use App\Services\Member\Models\User;
use Illuminate\Support\Facades\Auth;


class PlanFragment extends Base
{


    protected $table = 'plan_fragment';

    public const STATUS_PENDING = -1;
    public const STATUS_REFUSE = 1;
    public const STATUS_VERIFIED = 2;


    public const STATUS_TEXT = [
        self::STATUS_PENDING => '待审核',
        self::STATUS_REFUSE => '拒绝',
        self::STATUS_VERIFIED => '通过验收',

    ];

    protected $appends = [
        'status_text','isThumbsup'
    ];


    protected $fillable = [
        'user_id', 'plan_id','title',"thumbsup_count",'favorite_count','comments_count','last_different_weight','total_different_weight','weight'
    ];

    protected $hidden = [
        'deleted_at',
    ];

    public function getStatusTextAttribute()
    {
        return $this->statusText();
    }

    public function statusText(): string
    {
        return self::STATUS_TEXT[$this->status] ?? '';
    }

    public function getIsThumbsupAttribute()
    {
        $userId = Auth::guard('apiv2')->id(); // 获取当前登录用户
        $isThumbsup = false;
        if ($userId) {
            return $this->thumbsups->contains('user_id', $userId);
        }
        return $isThumbsup;
    }
    //发布动态的人
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }


    public function conquer()
    {
        return $this->belongsTo(Conquer::class, 'conquer_id');
    }



    public function images()
    {
        return $this->morphMany(Image::class, 'imageable');
    }


    public function videos()
    {
        return $this->morphToMany(Video::class, 'videoable');
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

    public function thumbsups()
    {
        return $this->morphMany(ThumbsUp::class, 'thumbsupable');
    }

    public  function  activity()
    {
        return $this->morphMany(Activity::class, 'activityable');
    }







}
